<template>
  <div class="setting-container">
    <el-card class="box-card">
      <el-tabs v-model="activeName" @tab-click="corporateMessage">
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理" name="first">
          <!-- 添加角色按钮 -->
          <el-button
            type="primary"
            icon="el-icon-plus"
            class="addRoleBut"
            @click="addRole"
          >
            新增角色
          </el-button>
          <el-table :data="tableData" style="width: 100%;" row-key="id" border>
            <el-table-column type="index" label="序号" width="180" />
            <el-table-column prop="name" label="角色名" sortable width="180" />
            <el-table-column
              prop="description"
              label="描述"
              sortable
              min-width="180"
            />
            <el-table-column
              prop="address"
              fixed="right"
              label="操作"
              width="300"
              align="center"
            >
              <template v-slot:default="{ row }">
                <el-button
                  type="text"
                  @click="allotClick(row.id)"
                >分配权限</el-button>
                <el-button type="text" @click="alter(row.id)">修改</el-button>
                <el-popconfirm
                  title="确定删除这个角色吗？"
                  @onConfirm="affirmDel(row.id)"
                >
                  <el-button
                    slot="reference"
                    type="text"
                    style="margin-left:10px"
                  >删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pagesize"
            layout="total, prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>
        <!-- 管理配置 -->
        <el-tab-pane label="公司信息" name="second">
          <el-form ref="form" :model="corporate" label-width="80px">
            <el-form-item label="企业名称">
              <el-input v-model="corporate.name" disabled />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="corporate.companyAddress" disabled />
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input v-model="corporate.companyPhone" disabled />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="corporate.mailbox" disabled />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="corporate.remarks" disabled />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <add-alter-dialog ref="stgDialog" />
    <per-dialog ref="PerDialog" />
  </div>
</template>

<script>
// 导入抽取的api接口
import {
  getCorporate,
  getRoleData,
  getRoleContent,
  delRoleContent
} from '@/api/setting'
import { mapGetters } from 'vuex'
import addAlterDialog from './settingList/add-alterDialog.vue'
import PerDialog from './settingList/perDialog.vue'
import { getPermissionList } from '../../api/permission'
import { formatData } from '../../utils/index'
export default {
  name: 'Setting',
  components: { addAlterDialog, PerDialog },
  data() {
    return {
      activeName: 'first',
      // 公司信息
      corporate: {
        name: ''
      },
      currentPage: 1,
      // 角色信息
      tableData: [],
      // 每页显示条数
      pagesize: 3,
      // 总条数
      total: 1
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  // 页面打开调用获取角色的方法
  created() {
    this.getRoleList()
  },
  methods: {
    // 设置方法获取角色信息
    async getRoleList() {
      const res = await getRoleData(this.currentPage, this.pagesize)
      this.tableData = res.rows
      this.total = res.total
    },
    // 点击tap调用的方法
    async corporateMessage(tab) {
      if (tab.$options.propsData.label === '公司信息') {
        if (this.corporate.name === '') {
          const res = await getCorporate(this.userInfo.companyId)
          this.corporate = res
        } else {
        }
      }
    },
    // 点击分页按钮实现切换数据显示
    async handleCurrentChange(val) {
      const res = await getRoleData(val, this.pagesize)
      this.tableData = res.rows
    },
    // 点击按钮显示对话弹窗添加角色
    addRole() {
      this.$refs.stgDialog.dialogFormVisible = true
    },
    // 点击按钮显示对话框修改角色
    async alter(id) {
      const res = await getRoleContent(id)
      this.$refs.stgDialog.form = res
      this.$refs.stgDialog.dialogFormVisible = true
      // console.log(data)
    },
    // 点击按钮删除角色
    async affirmDel(id) {
      await delRoleContent(id)
      // 判断删除的是不是当前页的最后一条数据
      if (this.tableData.length === 1 && this.currentPage > 1) {
        // 是的话页码减一
        this.currentPage--
      }
      this.getRoleList()
    },
    async allotClick(id) {
      this.$refs.PerDialog.roleId = id
      this.$refs.PerDialog.permData = formatData(await getPermissionList(), '0')
      const res = await getRoleContent(id)
      this.$refs.PerDialog.selectCheck = res.permIds
      this.$refs.PerDialog.visible = true
    }
  }
}
</script>

<style lang="scss">
.setting-container {
  padding: 20px;
  #pane-first {
    padding: 0 30px 20px;
    .addRoleBut {
      padding: 10px;
      margin: 30px 0 20px;
    }
    .el-pagination {
      margin-top: 30px;
    }
  }
  #pane-second {
    padding: 10px 100px 30px;
    .el-form-item {
      margin-bottom: 15px;
      .el-input {
        width: 400px;
      }
    }
  }
}
</style>
